/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hvkdbmc.setting;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LuckyMan
 */
public class HVKResultSet2Array {

    private ArrayList arrResultl;
    private ResultSetMetaData rsmd;

    public ArrayList getArrayListResult(ResultSet rs) {
        arrResultl = new ArrayList();
        try {            
            int cols = getColumnCount(rs);
            while (rs.next()) {
                Object[] row = new Object[cols];
                for (int j = 0; j < row.length; j++) {
                    row[j] = rs.getObject(j + 1);
                }
                arrResultl.add(row);
            }
        } catch (SQLException ex) {
            Logger.getLogger(HVKResultSet2Array.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        return arrResultl;
    }

    public Object getValueAt(int r, int c) {
        if (r < arrResultl.size()) {
            return ((Object[]) arrResultl.get(r))[c];
        } else {
            return null;
        }
    }

    public int getRowCount() {
        return arrResultl.size();
    }

    public String getColumnName(int c) {
        try {
            return rsmd.getColumnName(c + 1);
        } catch (SQLException e) {
            System.out.println("Error " + e);
            return "";
        }
    }

    public int getColumnCount(ResultSet rs) {
        try {
            return rs.getMetaData().getColumnCount();
        } catch (SQLException e) {
            System.out.println("Error " + e);
            return 0;
        }
    }
}
